<?php
namespace app\admin\controller;

use app\admin\model\ProductModel;
use think\cache\driver\Redis;
use think\Db;
use think\Request;

class Product extends Base
{
    public function lists (Request $request)
    {
        $type = $request->route ('type');
        $ProductModel = new ProductModel();
        $list = $ProductModel->getList (['pt.type' => $type], $request->get ('p'), 10);
        return view ('lists', [
            'list' => $list,
            'type' => $type,
        ]);
    }

    public function detail (Request $request)
    {
        $ProductModel = new ProductModel();
        if ($request->isPost ()) {
            //修改逻辑
            $id = $request->get ('id');

        } else {
            //查看逻辑
            $id = $request->get ('id');
            $type = $request->route ('type');
            $productType = Db::table ('tps_product_type')->field ('id,name AS type_name')->where (['type' => $type])->select ();
            if (empty($id)) $detail = ['id' => '', 'name' => '', 'author' => '', 'url' => '', 'description' => '', 'content' => '', 'pic' => '', 'status' => '', 'type' => ''];
            else $detail = $ProductModel->getDetails (['p.id' => $id]);
            return view ('detail', [
                'detail'   => $detail,
                'productType' => $productType
            ]);
        }
    }

    public function typeList(Request $request){
        $type = $request->route ('type');
        $productTypeList = Db::table ('tps_product_type')->field ('id,name AS type_name')->where (['type' => $type])->paginate (10);
        return view ('type_list', [
            'list' => $productTypeList
        ]);

    }
}
